package etm.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import upc.condominio.modelo.ReservaHoras;
import upc.condominio.util.ConexionBD;
import etm.excepcion.DAOExcepcion;

public class ReservaHorasDAO extends BaseDAO {

	
	public Collection<ReservaHoras> listar() throws DAOExcepcion {
		System.out.println("ReservaHorasDAO: listar()");
		Collection<ReservaHoras> c = new ArrayList<ReservaHoras>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ReservaHoras vo = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idrhoras, deshoras from ReservaHoras";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				vo = new ReservaHoras();				
				vo.setIdrhoras(rs.getInt("idrhoras"));
				vo.setDeshoras(rs.getString("deshoras"));
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public ReservaHoras buscarporId(int intHora) throws DAOExcepcion {
		ReservaHoras res =null;
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			String StrQuery = 	"SELECT idrhoras, deshoras from ReservaHoras where idrhoras=?";
			stmt = con.prepareStatement(StrQuery);
			stmt.setInt(1, intHora);
			rs = stmt.executeQuery();
			while (rs.next()) {
				res = new ReservaHoras();
				res.setIdrhoras(rs.getInt(1));
				res.setDeshoras(rs.getString(2));
				break;
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return res;
	}
	
	public Collection<ReservaHoras> listarDisponibles(Date fecReserva, int idAmbiente) throws DAOExcepcion {
		System.out.println("ReservaHorasDAO: listarDisponibles()");
		Collection<ReservaHoras> c = new ArrayList<ReservaHoras>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ReservaHoras vo = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idrhoras, deshoras FROM RESERVAHORAS WHERE IDRHORAS NOT IN (SELECT IDRHORAS FROM RESERVA WHERE FECHAINICIO = ? AND IDAMBIENTE = ?)";

			stmt = con.prepareStatement(query);
			stmt.setDate(1, fecReserva);
			stmt.setInt(2, idAmbiente);
			rs = stmt.executeQuery();
			while (rs.next()) {
				vo = new ReservaHoras();				
				vo.setIdrhoras(rs.getInt("idrhoras"));
				vo.setDeshoras(rs.getString("deshoras"));
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	
	
}
